// 工具类
const fun = {
    isNull(val) {  //判断字符串是否有效
        if (val === null || val === 'undefined' || val === '') {
            return true
        }
        return false;
    },
    isObjNotNull(obj) {  //判断对象是否有效
        if (obj != null && obj != 'undefined') {
            return true;
        }
        return false;
    },
    isString(val) {  //待验证
        return typeof val === 'string';
    },
    isNumber(val) {  //待验证
        return typeof val === 'number';
    },
    isUndefined(val) {  //待验证
        return typeof val === 'undefined';
    },
    getCompanyIdArr(obj) {  //遍历获取单位id拼接字符串，使用逗号隔开
        let idArr = [];
        if (!this.isObjNotNull(obj)) {
            return idArr;
        }
        idArr.push(obj.id);
        obj.children.forEach(x => {
            idArr.push(x.id);
            if (x.children.length > 0) {
                x.children.forEach(c => {
                    idArr.push(c.id);
                });
            }
        });
        return idArr;
    },
    getComIdArrList(list, comId) {  //根据提供的单位id获取当前单位及下级单位id集合
        let idArr = [];
        if (!this.isObjNotNull(list)) {
            return idArr;
        }
        if (this.isNull(comId)) {  //未提供单位id则拼接所有单位id
            list.forEach(x => {
                idArr.push(x.id);
            });
        } else {
            let chiList = list.find(x => x.id === comId);  //查询提供单位id是否在集合中
            if (this.isObjNotNull(chiList)) {
                idArr.push(chiList.id);  //当前单位id
                this.comRecursionIdArr(idArr, list, chiList.id);
            }
        }
        return idArr;
    },
    comRecursionIdArr(idArr, list, upComId) {  //递归获取单位id数组
        let chiList = list.filter(x => x.upId === upComId);
        if (this.isObjNotNull(chiList)) {
            chiList.forEach(x => {
                idArr.push(x.id);
                this.comRecursionIdArr(idArr, list, x.id);
            });
        }
    },
    companyTreeToList(comTree) {  //单位树状集合转数组
        let comList = [];
        if (!this.isObjNotNull(comTree)) {
            return comList;
        }
        comList.push({  //一级单位，也是自己单位
            id: comTree.id,
            upId: comTree.upId,
            name: comTree.name
        });
        comTree.children.forEach(x => {
            //二级单位
            comList.push({  //一级单位，也是自己单位
                id: x.id,
                upId: x.upId,
                name: x.name
            });
            if (x.children.length > 0) {
                x.children.forEach(c => {
                    comList.push({
                        id: c.id,
                        upId: c.upId,
                        name: c.name
                    });
                });
            }
        });
        return comList;
    },
    getListIdArr(list) {  //获取集合id数组
        let idArr = [];
        if (this.isObjNotNull(list)) {
            list.forEach(x => {
                idArr.push(x.id);
            });
        }
        return idArr;
    },
    longToDate(longDate) {  //日期转long
        if (this.isNull(longDate) || longDate === 0) {
            return "";
        }
        let dateTypeDate = "";
        let date = new Date();
        date.setTime(longDate);
        dateTypeDate += date.getFullYear(); //年
        dateTypeDate += "-" + this.getMonth(date); //月
        dateTypeDate += "-" + this.getDay(date); //日
        dateTypeDate += " " + this.getHours(date); //时
        dateTypeDate += ":" + this.getMinutes(date);  //分
        dateTypeDate += ":" + this.getSeconds(date);  //分
        return dateTypeDate;
    },
    longToDateNoTime(longDate) {  //日期转long
        if (this.isNull(longDate) || longDate === 0) {
            return "";
        }
        let dateTypeDate = "";
        let date = new Date();
        date.setTime(longDate);
        dateTypeDate += date.getFullYear(); //年
        dateTypeDate += "-" + this.getMonth(date); //月
        dateTypeDate += "-" + this.getDay(date); //日
        return dateTypeDate;
    },
    getMonth(date) {   //返回 01-12 的月份值
        let month = "";
        month = date.getMonth() + 1; //getMonth()得到的月份是0-11
        if (month < 10) {
            month = "0" + month;
        }
        return month;
    },
    getDay(date) {   //返回01-30的日期
        let day = "";
        day = date.getDate();
        if (day < 10) {
            day = "0" + day;
        }
        return day;
    },
    getHours(date) {  //小时
        let hours = "";
        hours = date.getHours();
        if (hours < 10) {
            hours = "0" + hours;
        }
        return hours;
    },
    getMinutes(date) {  //分
        let minute = "";
        minute = date.getMinutes();
        if (minute < 10) {
            minute = "0" + minute;
        }
        return minute;
    },
    getSeconds(date) {  //秒
        let second = "";
        second = date.getSeconds();
        if (second < 10) {
            second = "0" + second;
        }
        return second;
    },
    // 曲线图工具方法
    chart: {
        //初始化曲线容器
        iniChart(chart) {
            chart.setOption({
                grid: {
                    left: '35',
                    right: '10',
                    top: '15',
                    bottom: '27',
                },
                tooltip: {
                    trigger: 'axis'  //触发类型
                },
                xAxis: {
                    data: [],
                    axisLine: {  //坐标轴轴线相关设置
                        show: true,
                        lineStyle: {
                            color: '#0dd7ea',
                            width: 2
                        }
                    },
                    axisTick: {  //坐标轴刻度相关设置
                        show: true,
                        alignWithLabel: true,  //类目轴中在 boundaryGap 为 true 的时候有效，可以保证刻度线和标签对齐
                        length: 10,
                        lineStyle: {
                            color: '#0dd7ea',
                            width: 2
                        }
                    },
                    axisLabel: {  //坐标轴刻度标签的相关设置
                        show: true,
                        color: '#00c1df',
                        fontWeight: 'bold',
                        margin: 14
                    },
                },
                yAxis: {
                    axisLine: {  //坐标轴轴线相关设置
                        show: true,
                        lineStyle: {
                            color: '#0dd7ea',
                            width: 2
                        }
                    },
                    axisLabel: {  //坐标轴刻度标签的相关设置
                        show: true,
                        color: '#00c1df',
                        fontWeight: 'bold',
                        margin: 14
                    },
                    splitLine: {  //标轴在 grid 区域中的分隔线
                        show: true,
                        lineStyle: {
                            color: '#0dd7ea',
                            width: 1,
                            opacity: 0.2
                        }
                    },
                    axisTick: {  //坐标轴刻度相关设置
                        show: true,
                        alignWithLabel: true,  //类目轴中在 boundaryGap 为 true 的时候有效，可以保证刻度线和标签对齐
                        length: 10,
                        lineStyle: {
                            color: '#0dd7ea',
                            width: 2
                        }
                    },
                },
                series: []
            });
        },
        //添加曲线
        addChartData(chart, name, xData, yData, color, type, barWidth) {
            let _series = chart.getOption().series;
            _series.push(
                {
                    name: name,
                    type: type,
                    color: color,
                    data: yData,
                    barWidth: barWidth,
                    barMaxWidth: 30,
                    smooth: true,  //设置线条是否平滑
                    showSymbol: false,  //是否显示原点
                    connectNulls: false,  //是否连接null数据
                    label: {  //图形上的文本标签，可用于说明图形的一些数据信息
                        show: false
                    },
                    lineStyle: {
                        width: 2,
                    },
                    itemStyle: {  //图形样式
                        // color: color,
                        borderRadius: [3, 3, 0, 0],
                        opacity: 0.8,
                    },
                }
            );
            chart.setOption({
                xAxis: {
                    data: xData
                },
                series: _series
            });
        },
        //更新曲线数据
        updateChartData(chart, name, xData, yData) {
            chart.setOption({
                xAxis: {
                    data: xData
                },
                series: [
                    {
                        name: name,
                        data: yData
                    }
                ]
            });
        },
        //移除一条曲线数据
        removeChartData(chart, name, xData) {
            let _option = chart.getOption();
            let _series = chart.getOption().series;
            let index = _series.findIndex(x => x.name === name);
            if (index >= 0) {
                _series.splice(index, 1);
                _option.series = _series;
                //先更新曲线数据后更新x坐标，不可以一起更新
                chart.setOption(_option, true);
                chart.setOption({
                    xAxis: {
                        data: xData
                    }
                });
            }
        },
        //移除所有曲线
        removeAllChartData(chart) {
            let _option = chart.getOption();
            _option.series = [];
            //先更新曲线数据后更新x坐标，不可以一起更新
            chart.setOption(_option, true);
        },
        //曲线颜色分配
        getChartColor(list) {
            let colorArr = ['#e61b38', '#fbd444', '#2de0ad', '#9760e5', '#40a1ff', '#24f355', '#e849bb', '#ec6e1f'];
            let color = '';
            let index = -1;
            for (let i = 0; i < colorArr.length; i++) {
                color = colorArr[i];
                index = list.findIndex(x => x.color === color);
                if (index < 0) {
                    return color;
                }
            }
            return color;
        }
    }
};

export default fun



